package Personas;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nan
 */
public class MedModPer implements ActionListener, KeyListener{

    private GModificarPer Ingreso = new GModificarPer();
    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();
    private String clave = "";
    private personaListar persona;

    public MedModPer(){
        Ingreso.setLocationRelativeTo(null);
        Ingreso.setActionListeners(this);
        Ingreso.setKeyListener(this);
        Ingreso.show();
    }

    public MedModPer(personaListar aThis) {
        Ingreso.setLocationRelativeTo(null);
        Ingreso.setActionListeners(this);
        Ingreso.setKeyListener(this);
        Ingreso.show();
        this.persona = aThis;
    }

    public void actionPerformed(ActionEvent e) {
        
        if(e.getSource() == getIngreso().getjButtonModificar()){
            String a = (getIngreso().getjTextFieldNombre().getText());
            String b =(getIngreso().getjTextFieldApellido().getText());
            String c =(getIngreso().getjTextFieldDireccion().getText());
            String d = (getIngreso().getjTextFieldTelefono().getText());
            String ee =(getIngreso().getjTextFieldNombreUser().getText());
            String f =(getIngreso().getjTextFieldContra().getText());
            String g =(getIngreso().getTipoUser().getText());
            conectar.conectar();
            Statement st = null;
            try {
                st = conectar.getConn().createStatement();
                String modifica = "UPDATE \"Personas\" SET \"Nombre\"='"+a+"', \"Apellido\"='"+b+"', \"Direccion\"='"+c+"', \"Telefono\"='"+d+"', \"NomUsuario\"='"+ee+"', \"Contrasenia\"='"+f+"', \"TipoUser\"='"+g+"' WHERE \"DNI\" = '"+getClave()+"';";
                st.execute(modifica);
                st.close();
                vaciarCampos();
                this.persona.actualizarTabla();
                Ingreso.dispose();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Nose puede Modificar", "Error al modificar", JOptionPane.ERROR_MESSAGE);
            }
            conectar.cerrar();
        }
        if(e.getSource() == getIngreso().getjButtonCancelar()){
            getIngreso().dispose();
        }
    }

    public void keyTyped(KeyEvent e) {
        /*if (e.getSource()== getIngreso().getjTextFieldStockMin()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
        if (e.getSource()== getIngreso().getjTextFieldStockMax()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
        if (e.getSource()== getIngreso().getjTextFieldprecioCosto()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) && (caracter != '.')){
                e.consume();
            }
            if (caracter == '.' && getIngreso().getjTextFieldprecioCosto().getText().contains(".")){
                e.consume();
            }
        }
        if (e.getSource()== getIngreso().getStock()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }*/
    }

    public void keyPressed(KeyEvent e) {   }

    public void keyReleased(KeyEvent e) {   }

    public void vaciarCampos(){
        getIngreso().getjTextFieldNombre().setText("");
        getIngreso().getjTextFieldApellido().setText("");
        getIngreso().getjTextFieldDireccion().setText("");
        getIngreso().getjTextFieldTelefono().setText("");
        getIngreso().getjTextFieldNombreUser().setText("");
        getIngreso().getjTextFieldContra().setText("");
        getIngreso().getTipoUser().setText("");
        setClave("");
    }


    /**
     * @return the clave
     */
    public String getClave() {
        return clave;
    }

    /**
     * @param clave the clave to set
     */
    public void setClave(String clave) {
        this.clave = clave;
    }

    public void setVisible(boolean mostrar){
        Ingreso.setVisible(mostrar);
    }

    public GModificarPer getIngreso(){
        return Ingreso;
    }
   
}
